package logic

import (
	"context"
	"fmt"
	"gitcode.com/sunsungk9/opensource-demo-cockroachdb-obs-241123/user/internal/svc"
	"gitcode.com/sunsungk9/opensource-demo-cockroachdb-obs-241123/user/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackupLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBackupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackupLogic {
	return &BackupLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BackupLogic) Backup() (resp *types.Response, err error) {
	// backup to OBS
	query := fmt.Sprintf("BACKUP INTO 's3://%s?AWS_ENDPOINT=%s&AWS_ACCESS_KEY_ID=%s&AWS_SECRET_ACCESS_KEY=%s'",
		l.svcCtx.Config.Bucket, l.svcCtx.Config.Endpoint, l.svcCtx.Config.AK, l.svcCtx.Config.SK)
	_, err = l.svcCtx.DB.Exec(query)
	if err != nil {
		return
	}
	resp = new(types.Response)
	resp.Message = "success"
	return
}
